package com.bdweb.service.impl;

import com.bdweb.dao.MedialDao;
import com.bdweb.dao.impl.MedialDaoimpl;
import com.bdweb.model.WEAK;
import com.bdweb.pojo.Result;
import com.bdweb.service.MedialService;

import com.bdweb. mokuai.UDPClient1;
import com.bdweb. mokuai.WakeOnLan;
import com.bdweb. mokuai.getUDPhex;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
@Controller
public class MedailServiceImpl implements MedialService {
    @Autowired
    private MedialDaoimpl medialDaoimpl;
    @Autowired
    private MySqlUpdater mySqlUpdater;


    private String ip;  // IP
    private int port;   //端口号

    @Override
    public Result listMedailOpen(int id) throws IOException, InterruptedException {
        //获取bd_meaial数据库中所有数据
        List<WEAK> allWeak = medialDaoimpl.listMedial();


        if (id < 4) {
            ip = allWeak.get(id).getIp();
            port = Integer.parseInt(allWeak.get(id).getPort());
            //开启控制码
            String opencode = allWeak.get(id).getOpencode();
            System.out.println(opencode);

            if (id == 0) {

                String[] ips = allWeak.get(id).getIp().split("-");

                for (int i = 0; i < ips.length; i++) {
                    // 投影机

                    // System.out.println(ip[i]);


                    Thread.sleep(200);

                    new getUDPhex("23 50 57 52 30 2c 31 21 ",ips[i],port);
                    Thread.sleep(200);
                    // 以十六进制发送
                    new UDPClient1(opencode, ips[i], port);
                }
                mySqlUpdater.updateValue("bd_medias", id + 1, "isopen", "1");
            }
            if (id == 1 || id == 2) {

                new WakeOnLan(opencode);

                mySqlUpdater.updateValue("bd_medias", id + 1, "isopen", "1");
            }
            if (id == 3) {
                new getUDPhex(opencode, ip, port);
                Thread.sleep(200);
                new getUDPhex("3A 30 30 31 30 30 30 42 31 30 30 30 31 30 30 30 31 33 44 0D 0A", ip, port);
                Thread.sleep(200);
                new getUDPhex("3A 30 30 31 30 30 30 42 32 30 30 30 31 30 30 30 31 33 43 0D 0A", ip, port);
                mySqlUpdater.updateValue("bd_medias", id + 1, "isopen", "1");
            }
            if(id==4){

                    new UDPClient1(opencode,ip,port);

            }
        }


        return new Result(200, "success", "开启OK");
    }

    @Override
    public Result listMedailClose(int id) throws IOException, InterruptedException {

        List<WEAK> allWeak = medialDaoimpl.listMedial();

        if (id >= 4) {
            int id1 = id - allWeak.size();


            ip = allWeak.get(id1).getIp();
            port = Integer.parseInt(allWeak.get(id1).getPort());

            // 关闭控制码
            String closecode = allWeak.get(id1).getClosecode();

            //关闭设备
            if (id == 4) {
                String[] ips = allWeak.get(id1).getIp().split("-");


                for (int i = 0; i < ips.length; i++) {
                    // System.out.println(ip[i]);
                    // 字符串发送
                    new UDPClient1(closecode, ips[i], port);
                    // 以十六进制发送


                    Thread.sleep(100);

                    new getUDPhex("23 50 57 52 30 2c 30 21",ips[i],port);

                }
                mySqlUpdater.updateValue("bd_medias", id1 + 1, "isopen", "0");
            }

            if (id == 5 || id == 6) {
                new UDPClient1(closecode, ip, port);
                mySqlUpdater.updateValue("bd_medias", id1 + 1, "isopen", "0");

            }
            if (id == 7) {
                new getUDPhex(closecode, ip, port);
                Thread.sleep(200);
                new getUDPhex("3A 30 30 31 30 30 30 42 31 30 30 30 31 30 30 30 32 33 43 0D 0A", ip, port);
                Thread.sleep(200);
                new getUDPhex("3A 30 30 31 30 30 30 42 32 30 30 30 31 30 30 30 32 33 42 0D 0A", ip, port);
                mySqlUpdater.updateValue("bd_medias", id1 + 1, "isopen", "0");
            }
            if(id==8){
                new UDPClient1(closecode,ip,port);
            }
        }
        return new Result(200, "success", "关闭OK");
    }

    @Override
    public Result listMedailOpenAll() throws IOException, InterruptedException {
        String OpenCode1 = "3A 30 30 31 30 30 30 42 31 30 30 30 31 30 30 30 31 33 44 0D 0A";
        String OpenCode2 = "3A 30 30 31 30 30 30 42 32 30 30 30 31 30 30 30 31 33 43 0D 0A";
        // 多媒体设备全部开启

        return getResult(0, OpenCode1, OpenCode2, "1");
    }

    @Override
    public Result listMedailCloseAll() throws IOException, InterruptedException {
        String CloseCode1 = "3A 30 30 31 30 30 30 42 31 30 30 30 31 30 30 30 32 33 43 0D 0A";
        String CloseCode2 = "3A 30 30 31 30 30 30 42 32 30 30 30 31 30 30 30 32 33 42 0D 0A";

        // 多媒体设备全部关闭
        return getResult(1, CloseCode1, CloseCode2, "0");

    }

    private Result getResult(int id, String code1, String code2, String isopen) throws IOException, InterruptedException {
        List<WEAK> allWeak = medialDaoimpl.listMedial();

        for (int i = 0; i < allWeak.size(); i++) {


            String ip = allWeak.get(i).getIp();
            int port = Integer.parseInt(allWeak.get(i).getPort());

          //三元表达式
            String code = id <= 0 ? allWeak.get(i).getOpencode() : allWeak.get(i).getClosecode();
            System.out.println(code);
            if (i == 0) {
                String[] ips = ip.split("-");

                for (int j = 0; j < ips.length; j++) {

                    // 以十六进制发送


                    if (id == 0) {
                        new getUDPhex("23 50 57 52 49 44 2C 31 21 ", ips[j], port);

                        Thread.sleep(200);
                        new UDPClient1(code, ips[j], port);
                    } else {
                            new UDPClient1(code, ips[j], port);
                        Thread.sleep(200);
                        new getUDPhex("23 50 57 52 49 44 2C 30 21 ", ips[j], port);
                    }

                    //  new TCPClient(ips[j], port, code);

                }
                mySqlUpdater.updateValue("bd_medias", i + 1, "isopen", isopen);
            }
            if (i == 1 || i == 2) {
                if (id == 0) {
                    new WakeOnLan(code);
                } else {
                    new UDPClient1(code, ip, port);
                }


                mySqlUpdater.updateValue("bd_medias", i + 1, "isopen", isopen);
            }

            if (i == 3) {
                if (id == 0) {
                    new UDPClient1(code, ip, port);
                    Thread.sleep(200);
                    new UDPClient1(code1, ip, port);
                    Thread.sleep(200);
                    new UDPClient1(code2, ip, port);
                } else {
                    new UDPClient1(code, ip, port);
                    Thread.sleep(200);
                    new UDPClient1(code1, ip, port);
                    Thread.sleep(200);
                    new UDPClient1(code2, ip, port);
                }


                mySqlUpdater.updateValue("bd_medias", i + 1, "isopen", isopen);
            }

        }

        return new Result(200, "success", id == 0 ? "多媒体设备全部开启" : "多媒体设备全部关闭");
    }


}
